# LocalStack Resource Provider Scaffolding v2
from __future__ import annotations

from pathlib import Path
from typing import Optional, TypedDict

import localstack.services.cloudformation.provider_utils as util
from localstack.services.cloudformation.resource_provider import (
    OperationStatus,
    ProgressEvent,
    ResourceProvider,
    ResourceRequest,
)


class SchedulerScheduleProperties(TypedDict):
    FlexibleTimeWindow: Optional[FlexibleTimeWindow]
    ScheduleExpression: Optional[str]
    Target: Optional[Target]
    Arn: Optional[str]
    Description: Optional[str]
    EndDate: Optional[str]
    GroupName: Optional[str]
    KmsKeyArn: Optional[str]
    Name: Optional[str]
    ScheduleExpressionTimezone: Optional[str]
    StartDate: Optional[str]
    State: Optional[str]


class FlexibleTimeWindow(TypedDict):
    Mode: Optional[str]
    MaximumWindowInMinutes: Optional[float]


class DeadLetterConfig(TypedDict):
    Arn: Optional[str]


class RetryPolicy(TypedDict):
    MaximumEventAgeInSeconds: Optional[float]
    MaximumRetryAttempts: Optional[float]


class AwsVpcConfiguration(TypedDict):
    Subnets: Optional[list[str]]
    AssignPublicIp: Optional[str]
    SecurityGroups: Optional[list[str]]


class NetworkConfiguration(TypedDict):
    AwsvpcConfiguration: Optional[AwsVpcConfiguration]


class CapacityProviderStrategyItem(TypedDict):
    CapacityProvider: Optional[str]
    Base: Optional[float]
    Weight: Optional[float]


class PlacementConstraint(TypedDict):
    Expression: Optional[str]
    Type: Optional[str]


class PlacementStrategy(TypedDict):
    Field: Optional[str]
    Type: Optional[str]


class EcsParameters(TypedDict):
    TaskDefinitionArn: Optional[str]
    CapacityProviderStrategy: Optional[list[CapacityProviderStrategyItem]]
    EnableECSManagedTags: Optional[bool]
    EnableExecuteCommand: Optional[bool]
    Group: Optional[str]
    LaunchType: Optional[str]
    NetworkConfiguration: Optional[NetworkConfiguration]
    PlacementConstraints: Optional[list[PlacementConstraint]]
    PlacementStrategy: Optional[list[PlacementStrategy]]
    PlatformVersion: Optional[str]
    PropagateTags: Optional[str]
    ReferenceId: Optional[str]
    Tags: Optional[list[dict]]
    TaskCount: Optional[float]


class EventBridgeParameters(TypedDict):
    DetailType: Optional[str]
    Source: Optional[str]


class KinesisParameters(TypedDict):
    PartitionKey: Optional[str]


class SageMakerPipelineParameter(TypedDict):
    Name: Optional[str]
    Value: Optional[str]


class SageMakerPipelineParameters(TypedDict):
    PipelineParameterList: Optional[list[SageMakerPipelineParameter]]


class SqsParameters(TypedDict):
    MessageGroupId: Optional[str]


class Target(TypedDict):
    Arn: Optional[str]
    RoleArn: Optional[str]
    DeadLetterConfig: Optional[DeadLetterConfig]
    EcsParameters: Optional[EcsParameters]
    EventBridgeParameters: Optional[EventBridgeParameters]
    Input: Optional[str]
    KinesisParameters: Optional[KinesisParameters]
    RetryPolicy: Optional[RetryPolicy]
    SageMakerPipelineParameters: Optional[SageMakerPipelineParameters]
    SqsParameters: Optional[SqsParameters]


REPEATED_INVOCATION = "repeated_invocation"


class SchedulerScheduleProvider(ResourceProvider[SchedulerScheduleProperties]):
    TYPE = "AWS::Scheduler::Schedule"  # Autogenerated. Don't change
    SCHEMA = util.get_schema_path(Path(__file__))  # Autogenerated. Don't change

    def create(
        self,
        request: ResourceRequest[SchedulerScheduleProperties],
    ) -> ProgressEvent[SchedulerScheduleProperties]:
        """
        Create a new resource.

        Primary identifier fields:
          - /properties/Name

        Required properties:
          - FlexibleTimeWindow
          - ScheduleExpression
          - Target

        Create-only properties:
          - /properties/Name

        Read-only properties:
          - /properties/Arn

        IAM permissions required:
          - scheduler:CreateSchedule
          - scheduler:GetSchedule
          - iam:PassRole

        """
        model = request.desired_state

        if not model.get("Name"):
            model["Name"] = util.generate_default_name(
                request.stack_name, request.logical_resource_id
            )

        create_params = util.select_attributes(
            model,
            [
                "Description",
                "EndDate",
                "FlexibleTimeWindow",
                "GroupName",
                "KmsKeyArn",
                "Name",
                "ScheduleExpression",
                "ScheduleExpressionTimezone",
                "StartDate",
                "State",
                "Target",
            ],
        )

        result = request.aws_client_factory.scheduler.create_schedule(**create_params)
        model["Arn"] = result["ScheduleArn"]

        return ProgressEvent(
            status=OperationStatus.SUCCESS,
            resource_model=model,
        )

    def read(
        self,
        request: ResourceRequest[SchedulerScheduleProperties],
    ) -> ProgressEvent[SchedulerScheduleProperties]:
        """
        Fetch resource information

        IAM permissions required:
          - scheduler:GetSchedule
        """
        raise NotImplementedError

    def delete(
        self,
        request: ResourceRequest[SchedulerScheduleProperties],
    ) -> ProgressEvent[SchedulerScheduleProperties]:
        """
        Delete a resource

        IAM permissions required:
          - scheduler:DeleteSchedule
          - scheduler:GetSchedule
        """

        delete_params = util.select_attributes(request.desired_state, ["Name", "GroupName"])
        request.aws_client_factory.scheduler.delete_schedule(**delete_params)
        return ProgressEvent(status=OperationStatus.SUCCESS, resource_model={})

    def update(
        self,
        request: ResourceRequest[SchedulerScheduleProperties],
    ) -> ProgressEvent[SchedulerScheduleProperties]:
        """
        Update a resource

        IAM permissions required:
          - scheduler:UpdateSchedule
          - scheduler:GetSchedule
          - iam:PassRole
        """
        raise NotImplementedError
